
package com.kanjian.music.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.kanjian.music.R;
import com.kanjian.music.adapter.MusicListListAdapater;
import com.kanjian.music.api.ApiRequestManager;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.database.DatabaseManager;
import com.kanjian.music.database.MusicDao;
import com.kanjian.music.entity.Music;
import com.kanjian.music.network.HttpLoader;

import de.greenrobot.dao.WhereCondition.StringCondition;

public class MyFavMusicActivity extends BaseActivity implements LoaderCallbacks<String> {

    private List<Music> mLocalMusicList;

    private MusicDao mMusicDao;

    private LinearLayout mLayout_muiscList;

    private ListView mLv_musicList;

    private MusicListListAdapater mListAdapter;

    private LocalBroadcastManager mLocalBroadcastManager;

    @Override
    protected void onCreate(Bundle arg0) {
        setContentView(R.layout.activity_myfav_music);
        super.onCreate(arg0);
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
        registerBroadcastReceiver();
        initView();
    }

    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() != null) {
                if (intent.getAction().equals(IntentConstants.ACTION_ON_LIKESTATUS_CHANGED)) {
                    peddingLocalFavMusic();
                }
            }
        }
    };

    private void registerBroadcastReceiver() {
        IntentFilter intentFiler = new IntentFilter();
        intentFiler.addAction(IntentConstants.ACTION_ON_LIKESTATUS_CHANGED);
        mLocalBroadcastManager.registerReceiver(mBroadcastReceiver, intentFiler);
    }

    private void unregisterBroadcastReceiver() {
        mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver);
    }

    private void initView() {
        setTitle("我喜欢的音乐");
        mLayout_muiscList = (LinearLayout) findViewById(R.id.fav_music_layout);
        mLv_musicList = (ListView) mLayout_muiscList.findViewById(R.id.musiclist_listview);
        mListAdapter = new MusicListListAdapater(this, mLv_musicList);
        mLv_musicList.setAdapter(mListAdapter);
        mMusicDao = DatabaseManager.session.getMusicDao();
        peddingLocalFavMusic();
        Bundle b = new Bundle();
        b.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                ApiRequestManager.getMyFavorteRequest());
        getSupportLoaderManager().restartLoader(0, b, this);
    }

    private void peddingLocalFavMusic() {
        mLocalMusicList = mMusicDao.queryBuilder().where(new StringCondition(" IS_FAV = 0 "))
                .list();
        mListAdapter.setMusicList(mLocalMusicList);
        mListAdapter.notifyDataSetChanged();
        mLayout_muiscList.setVisibility(View.VISIBLE);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterBroadcastReceiver();
    }

    @Override
    public Loader<String> onCreateLoader(int id, Bundle args) {
        return new HttpLoader(this, args);
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        if (data != null) {
            ArrayList<Music> musicList = Music.getMusicListFromJson(data);
            if (musicList != null) {
                mMusicDao.insertOrReplaceInTx(musicList);
                peddingLocalFavMusic();
            }
        }
    }

    @Override
    public void onLoaderReset(Loader<String> loader) {
    }

}
